package es.xpt.ysf.commons.pos;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import es.xpt.ysf.configuration.Configuration;


public class PosCaixaDispatcher extends HttpServlet { // NO_UCD
	private static final long serialVersionUID = 1L;
	
    public PosCaixaDispatcher() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		perform(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		perform(request,response);		
	}	
	
	private void perform (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Hashtable<String, String> parameters = loadSpecialParameters(request);
		String action = (String)request.getParameter("action");
		if (action==null)
			action=parameters.get("action");
		
		if (action!=null && action.trim().equals("dispatch")) {
			dispatchCaixa(request,response);
		}
		
		if (action!=null && action.trim().equals("dispatchPost")) {
			dispatchCaixaPost(request, response);
		}
		
		if (action!=null && action.trim().equals("OK")) {
			dispatchOKKO(request,response,"OK", parameters);
		}
		
		if (action!=null && action.trim().equals("KO")) {
			dispatchOKKO(request,response,"KO", parameters);
		}
		
	}	
	
	private Hashtable<String, String> loadSpecialParameters(HttpServletRequest request) {
		String special = (String)request.getParameter("params");
		if (special!=null) {
			Hashtable<String, String> parameters = new Hashtable<String, String>();
			String[] params = special.split("-");
			
			for (String s:params) {
				String individualParam[] = s.split("=");
				parameters.put(individualParam[0], individualParam[1]);
			}
			return parameters;
		}
		return null;
	}

	private void dispatchOKKO(HttpServletRequest request,
			HttpServletResponse response,String action,
			Hashtable<String, String> parameters) throws IOException {
		String environment = parameters.get("env");
		String app = parameters.get("app");
		String operationNumber = parameters.get("bookingReference");		
		String price = parameters.get("price");
		String module = "modules."+app+".urlCaixaReturn";
		String url = (new Configuration (environment)).getString(module);
		url+="?action="+action+"&operationNumber="+operationNumber+"&price="+price;;
		response.sendRedirect(url);		
	}
	
	private void dispatchCaixa(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String url = (String)request.getParameter("urlPos");
		response.sendRedirect(url);
				
	}
	
	private void dispatchCaixaPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String url = (String)request.getParameter("urlPos");
		if (url==null || url.equals("")) {
			url = (String)request.getAttribute("urlPos");
		}
		PrintWriter pw = response.getWriter();
		pw.write( url );
		pw.flush();
		pw.close();

				
	}
}
